package com.cheyun.netsalev3.widget

import android.app.Dialog
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.MyContentDialogLayout2Binding
import kotlin.reflect.KFunction1
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import com.cheyun.netsalev3.bean.order.ClockData
import com.cheyun.netsalev3.bean.order.OrderDetail
import com.cheyun.netsalev3.utils.ToastUtil
import java.io.Serializable


class MyContentDialogLayoutOrder : DialogFragment() {
    lateinit var binding: MyContentDialogLayout2Binding

    var contentView: View? = null

    var iv_all:ImageView? = null
    var iv_loan:ImageView? = null
    var iv_license:ImageView? = null
    var iv_finish:ImageView? = null

    var ll_payment_all:LinearLayout? = null
    var ll_payment_loan:LinearLayout? = null
    var ll_license:LinearLayout? = null
    var ll_finish:LinearLayout? = null

    var clockData:ClockData? = null
    var hasHandover:Boolean = false
    var orderDetail:OrderDetail? = null


    /**系统调用这个来获得对话片段的布局，不管是否显示为对话框或嵌入片段。*/
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        //将布局膨胀为对话框或嵌入片段
        binding = DataBindingUtil.inflate(inflater, R.layout.my_content_dialog_layout2, container, false)
        contentView = inflater.inflate(content, null)

        iv_all = contentView?.findViewById<ImageView>(R.id.iv_all)
        iv_loan = contentView?.findViewById<ImageView>(R.id.iv_loan)
        iv_license = contentView?.findViewById<ImageView>(R.id.iv_license)
        iv_finish = contentView?.findViewById<ImageView>(R.id.iv_finish)

        ll_license = contentView?.findViewById<LinearLayout>(R.id.ll_license)
        ll_payment_all = contentView?.findViewById<LinearLayout>(R.id.ll_payment_all)
        ll_payment_loan = contentView?.findViewById<LinearLayout>(R.id.ll_payment_loan)
        ll_finish = contentView?.findViewById<LinearLayout>(R.id.ll_finish)

        initView()
        binding.conetenView.addView(contentView)
        binding.setLifecycleOwner(this)

        return binding.root
    }


    private fun initView() {
        binding.cancelButton.setOnClickListener {
            dismiss()
            onClickCancel?.apply { this() }
        }
        binding.okButton.setOnClickListener {
            dismiss()
            onClickOk?.apply {
                this(clockData!!)
            }
        }
//        binding.conetenTex.text = content
        binding.titleText.text = title

        if(!hasHandover) {
            ll_payment_loan?.setOnClickListener {
                clockData?.ext?.payment = "loan"
                setData()
            }
            ll_payment_all?.setOnClickListener {
                clockData?.ext?.payment = "all"
                setData()
            }
        }
        ll_license?.setOnClickListener {
            clockData?.ext?.license = if(clockData?.ext?.license.equals("1")) "0" else "1"
            setData()
        }
        ll_finish?.setOnClickListener {
            if(orderDetail?.is_servicing == 1 && orderDetail?.appstate == 3){
                ToastUtil.toastShortMessage("已整备完成，不能取消完成签署打卡")
            }else {
                clockData?.finished = if (clockData?.finished == 1) 0 else 1
                setData()
            }
        }

        callbackInitView?.apply {
            this(binding)
        }

        setCancelable(touchCancel)

        setData()
    }

    fun setData(){
        if(clockData?.ext!=null){
            if(clockData?.ext?.payment.equals("loan")){
                iv_all?.setImageResource(R.drawable.weixuanzhong)
                iv_loan?.setImageResource(R.drawable.yixuanzhong)
            }else{
                iv_loan?.setImageResource(R.drawable.weixuanzhong)
                iv_all?.setImageResource(R.drawable.yixuanzhong)
            }
            iv_license?.setImageResource(if(clockData?.ext?.license.equals("1")) R.drawable.xz_tb else R.drawable.wxz_tb )
        }
        iv_finish?.setImageResource(if(clockData?.finished==1) R.drawable.xz_tb else R.drawable.wxz_tb )
    }


    /**系统仅在对话框中创建布局时调用此操作。*/
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        val window = dialog.window
        window.setBackgroundDrawableResource(android.R.color.transparent)

        return dialog
    }

    var title = ""
    var content = 0
    var touchCancel = true
    var onClickOk: ((ClockData) -> Unit)? = null
    var onClickCancel: (() -> Unit)? = null
    var callbackInitView: KFunction1<@ParameterName(name = "binding") MyContentDialogLayout2Binding, Unit>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            title = it.getString("title")
            content = it.getInt("content")
            touchCancel = it.getBoolean("touchCancel")
            clockData = it.getSerializable("clockData") as ClockData
            hasHandover = it.getBoolean("hasHandover")
            orderDetail = it.getSerializable("orderDetail") as OrderDetail
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(
                title: String,
                content: Int,
                touchCancel:Boolean = true,
                clockData: ClockData,
                hasHandover:Boolean,
                orderDetail: OrderDetail) =
                MyContentDialogLayoutOrder().apply {
                    arguments = Bundle().apply {
                        putString("title", title)
                        putInt("content", content)
                        putBoolean("touchCancel", touchCancel)
                        putSerializable("clockData",clockData as Serializable)
                        putBoolean("hasHandover",hasHandover)
                        putSerializable("orderDetail",orderDetail as Serializable)
                    }
                }
    }


}